package XComponent.show
{
	import XComponent.hide.XEvent;
	
	import fl.containers.UILoader;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;

	public class XLoader	extends XSprite
	{
		static public const DEFAULTURL : String = "assets/img/default/Loader.png";
		static public const LOAD_COMPLETE : String = "LOAD_COMPLETE";
		
		private var m_UILoader : UILoader;
		
		public function XLoader()
		{
			super();
		}
		
		public function load(url:String="",context:LoaderContext=null) : void
		{
			if(!url)	url = DEFAULTURL;
			
			m_UILoader = new UILoader;
			
			this.addChild(this.m_UILoader);
			
			m_UILoader.load(new URLRequest(url),context);
			
			m_UILoader.addEventListener(Event.COMPLETE,OnLoadComplete);
		}
		
		private function OnLoadComplete(evt : Event) : void
		{
			m_UILoader.removeEventListener(Event.COMPLETE,OnLoadComplete);

			m_UILoader.width =  m_UILoader.content.width;
			m_UILoader.height = m_UILoader.content.height;
			
			m_UILoader.unload();
			
			setSize(m_UILoader.width,m_UILoader.height);
			
			this.dispatchEvent(new XEvent(XLoader.LOAD_COMPLETE));
		}
		
		public function get loader() : UILoader
		{
			return m_UILoader;
		}
	}
}